<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ExternalContact;
use App\Traits\Admin\SuccessOrFailure;
use Illuminate\Http\Request;

class ExternalContactController extends Controller
{
    use SuccessOrFailure;

    private $permission = 'common.external_contact';


    /**
     *  外部联系人
     * @param Request $request
     * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/18 16:01
     */
    public function index(Request $request)
    {
        $this->canOrFailure($this->permission);

        $category = $request->input('category');
        if (!$category) {
            return $this->failure('参数错误');
        }
        $data = ExternalContact::query()->when($request->input('category'), function ($query) use ($request) {
            return $query->where('category', $request->input('category'));
        })->paginate();

        $name = ExternalContact::CATEGORY_MAP[$category] ??'';

        $is_source_contact = $category === ExternalContact::CATEGORY_SOURCE_CONTACT;

        return view('admin.external_contact.index', compact('data', 'category','name','is_source_contact'));
    }


    /**
     * 添加联系人
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     * @Author: hql <858566651@qq.com>
     * @DateTime: 2021/12/18 16:07
     */
    public function create(Request $request)
    {
        $this->canOrFailure($this->permission);
        $is_source_contact = $request->input('category') === ExternalContact::CATEGORY_SOURCE_CONTACT;
        return view('admin.external_contact.create',compact('is_source_contact'));
    }

    public function store(Request $request)
    {
        $this->canOrFailure($this->permission);

        $attributes = $this->buildData($request);
        ExternalContact::create($attributes);
        return $this->success('', route('admin.signCompany.index',['category'=>$request->input('category')]));
    }

    public function edit($id)
    {
        $this->canOrFailure($this->permission);
        $external_contact = $this->getExternalContactOrFailure($id);
        $is_source_contact = $external_contact->category == ExternalContact::CATEGORY_SOURCE_CONTACT;
        return view('admin.external_contact.edit', compact('external_contact','is_source_contact'));
    }

    public function update(Request $request, $id)
    {
        $this->canOrFailure($this->permission);

        $external_contact = $this->getExternalContactOrFailure($id);
        $attributes = $this->buildData($request);

        $external_contact->update($attributes);

        return $this->success('', route('admin.externalContact.index',['category'=>$request->input('category')]));
    }

    public function buildData($request)
    {
        return [
            'category' => $request->input('category'),
            'project_source' => $request->input('project_source'),
            'unit' => $request->input('unit'),
            'username' => $request->input('username'),
            'mobile' => $request->input('mobile'),
            'related_uid' => $request->input('related_uid'),
        ];
    }

    public function destroy($id)
    {
        $this->canOrFailure($this->permission);

        $external_contact = $this->getExternalContactOrFailure($id);
        $external_contact->delete();
        return $this->success();
    }

    private function getExternalContactOrFailure($id)
    {
        $external_contact = ExternalContact::query()->find($id);
        if (!$external_contact) {
            response($this->failure('值不存在！'), 404)->send();
            exit;
        }
        return $external_contact;
    }
}
